﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;

namespace NetCoreCMS.Web.Areas.Admin.Controllers
{
    public class LoginController : AreasController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Login.cshtml";
        private readonly string ManagerSignInErrorTimes = "ManagerSignInErrorTimes";
        private readonly int MaxErrorTimes = 5;
        private readonly IManagerService _manager;
        private readonly IManager_logService _manager_log;
        private readonly IWebHostEnvironment _env;
        public LoginController(IManagerService service, IManager_logService manager_log, IWebHostEnvironment env)
        {
            _manager = service;
            _manager_log = manager_log;
            _env = env;
        }
        [HttpGet]
        [ActionName("Index")]
        public IActionResult Index()
        {
            ViewData["config"] = Common.XMLConfig.Sysconfig.loadConfig(_env.ContentRootPath);
            ViewBag.msgtip = "请输入用户名和密码";
            return View("~/Areas/Admin/Views/Login.cshtml");
        }

        [HttpPost]
        public async Task<IActionResult> SubmitLogin()
        {
            ViewData["config"] = Common.XMLConfig.Sysconfig.loadConfig(_env.ContentRootPath);
            ActionResult result = View(WEB_VIEW);
            string userName = Request.Form["txtUserName"];
            string userPwd =  Request.Form["txtPassword"];

            if (userName.Equals("") || userPwd.Equals(""))
            {
                ViewBag.msgtip = "请输入用户名或密码";
                return result;
            }
            #region 判断错误次数
            var ErrorTimes = HttpContext.Session.GetInt32(ManagerSignInErrorTimes);
            if (ErrorTimes == null)
            {
                HttpContext.Session.SetInt32(ManagerSignInErrorTimes, 1);
                ErrorTimes = 1;
            }
            else
            {
                HttpContext.Session.SetInt32(ManagerSignInErrorTimes, ErrorTimes.Value + 1);
            }
            if (ErrorTimes > MaxErrorTimes)
            {
                ViewBag.msgtip = "错误超过5次，关闭浏览器重新登录！";
                return result;
            }
            #endregion
            var model = _manager.GetModel(userName, userPwd, true);
            if (model == null)
            {
                ViewBag.msgtip = "用户名或密码有误，请重试！";
                return result;
            }
            HttpContext.Session.Set(CMSKeys.SESSION_ADMIN_INFO, ByteConvertHelper.Object2Bytes(model));
            //写入登录日志
            var sysConfig = Common.XMLConfig.Sysconfig.loadConfig(_env.ContentRootPath);
            if (sysConfig.logstatus > 0)
            {
               await _manager_log.AddAsync(model.id, model.user_name, CMSEnums.ActionEnum.Login.ToString(), "用户登录",CMSRequest.GetClientUserIp(HttpContext));
            }
            //写入Cookies
            HttpContext.Response.Cookies.Append("DTRememberName", model.user_name);
            HttpContext.Response.Cookies.Append("AdminName", model.user_name);
            HttpContext.Response.Cookies.Append("AdminPwd", model.password);
            return Redirect("../Index/Index");
        }
    }
}